#include "MainWindow.h"
#include<customwidget.h>
#include <QFile>
#include <QDataStream>
#include <QIODevice>
#include <QDir>
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("Circuity");
    this->setWindowIcon(QIcon(":/pngsucai_4381385_948bed.png"));
    ui->graphicsView->scene->setSceneRect(ui->graphicsView->rect());
    ui->tabWidget->setTabText(0,"A");
    ui->tabWidget->setTabText(1,"G_n");
    ui->tabWidget->setTabText(2,"U_n");
    ui->widget_2->btnText->setText("AddArc");
    connect(ui->widget_2,&textButton::clicked,this,[=]{
        if(ui->graphicsView->start!=nullptr&&ui->graphicsView->end!=nullptr){
            ui->graphicsView->AddArc(ui->graphicsView->start,ui->graphicsView->end);
            ui->graphicsView->init();
            ui->graphicsView->start=nullptr;
            ui->graphicsView->end=nullptr;
        }
        else
            qDebug()<<"MissingVertex";
    });
    ui->widget_3->btnText->setText("DelArc");
    connect(ui->widget_3,&textButton::clicked,this,[=]{
        if(ui->graphicsView->start!=nullptr&&ui->graphicsView->end!=nullptr){
            ui->graphicsView->DelArc(ui->graphicsView->HasConnection(ui->graphicsView->start,ui->graphicsView->end));
            ui->graphicsView->init();
            ui->graphicsView->start=nullptr;
            ui->graphicsView->end=nullptr;
        }
        else
            qDebug()<<"MissingVertex";
    });
    ui->widget_4->btnText->setText("EditZ");
    connect(ui->widget_4,&textButton::clicked,this,[=]{
        if(ui->graphicsView->start!=nullptr&&ui->graphicsView->end!=nullptr){
            qreal Z=QInputDialog::getDouble(this,"InputZ","Z");
            for(int i=0;i<ui->graphicsView->EdgeCount;i++)
                if(ui->graphicsView->edge[i]->start==ui->graphicsView->start&&ui->graphicsView->edge[i]->end==ui->graphicsView->end){
                    ui->graphicsView->edge[i]->Z=Z;
                    ui->graphicsView->edge[i]->update();
                }
            ui->graphicsView->init();
            ui->graphicsView->start=nullptr;
            ui->graphicsView->end=nullptr;
        }
        else
            qDebug()<<"MissingVertex";
    });
    ui->widget_5->btnText->setText("CurSou");
    connect(ui->widget_5,&textButton::clicked,this,[=]{
        if(ui->graphicsView->start!=nullptr&&ui->graphicsView->end!=nullptr){
            ui->graphicsView->UpdateI_s();
            qreal I=QInputDialog::getDouble(this,"InputI","I");
            for(int i=0;i<ui->graphicsView->EdgeCount;i++)
                if(ui->graphicsView->edge[i]->start==ui->graphicsView->start&&ui->graphicsView->edge[i]->end==ui->graphicsView->end)
                    ui->graphicsView->edge[i]->I=I;
            ui->graphicsView->UpdateI_s();
            ui->graphicsView->init();
            ui->graphicsView->start=nullptr;
            ui->graphicsView->end=nullptr;

        }
        else
            qDebug()<<"MissingVertex";
    });
    ui->widget_6->btnText->setText("VolSou");
    connect(ui->widget_6,&textButton::clicked,this,[=]{
        if(ui->graphicsView->start!=nullptr&&ui->graphicsView->end!=nullptr){
            ui->graphicsView->UpdateU_s();
            qreal U=QInputDialog::getDouble(this,"InputU","U");
            for(int i=0;i<ui->graphicsView->EdgeCount;i++)
                if(ui->graphicsView->edge[i]->start==ui->graphicsView->start&&ui->graphicsView->edge[i]->end==ui->graphicsView->end)
                    ui->graphicsView->edge[i]->U=U;
            ui->graphicsView->UpdateU_s();
            ui->graphicsView->init();
            ui->graphicsView->start=nullptr;
            ui->graphicsView->end=nullptr;
        }
        else
            qDebug()<<"MissingVertex";
    });
    ui->widget_7->btnText->setText("ParSim");
    connect(ui->widget_7,&textButton::clicked,this,[=]{
        ui->graphicsView->ParSim();
    });
    ui->widget_8->btnText->setText("Show");
    connect(ui->widget_8,&textButton::clicked,this,[=]{
        ui->A->setRowCount(Vertex::VertexCount-1);
        ui->A->setColumnCount(ui->graphicsView->EdgeCount);
        ui->graphicsView->UpdateA();
        for (int i = 0; i < Vertex::VertexCount-1; ++i) {
            for (int j = 0; j < ui->graphicsView->EdgeCount; ++j) {
                qDebug()<<ui->graphicsView->A(i,j);
                QTableWidgetItem* item = new QTableWidgetItem(QString::number(ui->graphicsView->A(i,j)));
                ui->A->setItem(i, j, item);
            }
        }
        ui->G_n->setRowCount(Vertex::VertexCount-1);
        ui->G_n->setColumnCount(Vertex::VertexCount-1);
        ui->graphicsView->UpdateG();
        for(int i=0;i<Vertex::VertexCount-1;i++)
            for(int j=0;j<Vertex::VertexCount-1;j++){
                QTableWidgetItem* item=new QTableWidgetItem(QString::number(ui->graphicsView->G_n(i,j)));
                ui->G_n->setItem(i,j,item);
            }
        ui->U_n->setRowCount(Vertex::VertexCount-1);
        ui->U_n->setColumnCount(1);
        ui->graphicsView->solve();
        for(int i=0;i<Vertex::VertexCount-1;i++){
            QTableWidgetItem* item=new QTableWidgetItem(QString::number(ui->graphicsView->U_n(i,0)));
            ui->U_n->setItem(i,0,item);
        }
    });
    ui->widget_9->btnText->setText("SerSim");
    connect(ui->widget_9,&textButton::clicked,this,[=]{
        if(ui->graphicsView->start!=nullptr&&ui->graphicsView->end!=nullptr){
            ui->graphicsView->SerSim(ui->graphicsView->start,ui->graphicsView->end);
        }
        else
            qDebug()<<"MissingVertex";
    });
    ui->widget_10->btnText->setText("T->S");
    connect(ui->widget_10,&textButton::clicked,this,[=]{
        ui->graphicsView->state=1;
    });
    ui->widget_11->btnText->setText("SaveToFile");
    connect(ui->widget_11,&textButton::clicked,this,[=]{
        QString SavePath=QFileDialog::getSaveFileName(this,tr("save circuit")," ",tr("circuit(*.cir)"));
        if(!SavePath.isEmpty())
            SaveToFile(SavePath);
    });
    ui->widget_12->btnText->setText("ReadFromFile");
    connect(ui->widget_12,&textButton::clicked,this,[=]{
        QString InputPath = QFileDialog::getOpenFileName(this, tr("open circuit"), " ",  tr("circuit(*.cir)"));
        if(!InputPath.isEmpty())
            ReadFromFile(InputPath);
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::SaveToFile(const QString &path){
    QFile output(path);
    output.open(QIODevice::WriteOnly|QIODevice::Text);
    QTextStream ts(&output);
    ts<<"AKJdakjSDjfabad=\n";
    ui->graphicsView->SaveToFile(ts);
    output.close();
}

void MainWindow::ReadFromFile(const QString &path){
    QFile input(path);
    input.open(QIODevice::ReadOnly|QIODevice::Text);
    QTextStream ts(&input);
    ts.readLine();
    ui->graphicsView->ReadFromFile(ts);
    input.close();
}

